//                       .::::.
//                     .::::::::.
//                    :::::::::::
//                 ..:::::::::::'
//              '::::::::::::'
//                .::::::::::
//           '::::::::::::::..
//                ..::::::::::::.
//              ``::::::::::::::::
//               ::::``:::::::::'        .:::.
//              ::::'   ':::::'       .::::::::.
//            .::::'      ::::     .:::::::'::::.
//           .:::'       :::::  .:::::::::' ':::::.
//          .::'        :::::.:::::::::'      ':::::.
//         .::'         ::::::::::::::'         ``::::.
//     ...:::           ::::::::::::'              ``::.
//    ```` ':.          ':::::::::'                  ::::..
//                       '.:::::'                    ':'````..
package com.xw.controller.manage;

import com.github.pagehelper.PageHelper;
import com.xw.model.DicGroupLimit;
import com.xw.model.SysType;
import com.xw.service.TypeService;
import com.xw.service.sysconfig.DicGroupLimitService;
import com.xw.util.JsonData;
import com.xw.util.PropertiesUtil;
import com.xw.vo.TypeVo;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/manage/type/")
@Scope("prototype")
public class TypeController {
    @Resource
    private TypeService typeService;
    @Resource
    private DicGroupLimitService groupLimitService;



     /**
     * 根据父级id查询子集
     * @param stFId
     * @return
     */
    @RequestMapping(value = "findTypeByStFid",method = RequestMethod.POST)
    @ResponseBody
    public Map findTypeByStFid(String stFId){
        Map map = typeService.findTypeByStFid(stFId);
        return map;
    }

    /**
     * 编辑修改
     * @param request
     * @return
     */
    @RequestMapping(value = "updateOne",method = RequestMethod.POST)
    @ResponseBody
    public Map update(HttpServletRequest request, SysType sysType){
        Map map = typeService.updateOne(sysType);
        return map;
    }

    /**
     * 编辑修改
     * @param request
     * @return
     */
    @RequestMapping(value = "updateTwo",method = RequestMethod.POST)
    @ResponseBody
    public Map updateTwo(HttpServletRequest request, SysType sysType){
        Map map = typeService.updateTwo(sysType);
        return map;
    }

    /**
     * 跳转到编辑页面
     * @return
     */
    @RequestMapping(value = "goEditOne",method = RequestMethod.GET)
    public String goEditOne(HttpServletRequest request,String stId){
        SysType sysType = typeService.findByKeyId(stId);
        request.setAttribute("model",sysType);
        request.setAttribute("url", PropertiesUtil.getProperty("URL"));
        return "type/editOne";
    }

    /**
     * 跳转到编辑页面
     * @return
     */
    @RequestMapping(value = "goEditTwo",method = RequestMethod.GET)
    public String goEditTwo(HttpServletRequest request,String stId){
        //查询所有一级分类
        List<TypeVo> typeOneList = typeService.findTypeOne();
        SysType sysType = typeService.findByKeyId(stId);
        request.setAttribute("typeOneList",typeOneList);
        request.setAttribute("model",sysType);
        request.setAttribute("url", PropertiesUtil.getProperty("URL"));
        return "type/editTwo";
    }

    /**
     * 删除
     * @return
     */
    @RequestMapping(value = "del",method = RequestMethod.POST)
    @ResponseBody
    public Map del(String stId,Integer stLevel){
        Map map = typeService.del(stId,stLevel);
        return map;
    }

    /**
     * 跳转到新增一级分类页面
     * @return
     */
    @RequestMapping(value = "goAddOne",method = RequestMethod.GET)
    public String goAddOne(HttpServletRequest request){
        return "type/addOne";
    }

    /**
     * 跳转到新增二级分类页面
     * @return
     */
    @RequestMapping(value = "goAddTwo",method = RequestMethod.GET)
    public String goAddTwo(HttpServletRequest request){
        //查询一级分类
        List<TypeVo> typeOneList = typeService.findTypeOne();
        request.setAttribute("typeOneList",typeOneList);
        return "type/addTwo";
    }

    /**
     * 跳转到新增三级级分类页面
     * @return
     */
    @RequestMapping(value = "goAddThree",method = RequestMethod.GET)
    public String goAddThree(HttpServletRequest request){
        //查询一级分类
        List<TypeVo> typeOneList = typeService.findTypeOne();

        request.setAttribute("typeOneList",typeOneList);
        return "type/addThree";
    }

    /**
     * 添加商品分类
     * @return
     */
    @RequestMapping("addOne")
    @ResponseBody
    public Map addOne(String stName,Integer stSort){
        return typeService.addOne(stName,stSort);
    }

    /**
     * 添加商品分类
     * @return
     */
    @RequestMapping("addTwo")
    @ResponseBody
    public Map addTwo(String stName,Integer stSort,String stIdOne){
        return typeService.addTwo(stName,stSort,stIdOne);
    }

    /**
     * 添加商品分类
     * @return
     */
    @RequestMapping("addThree")
    @ResponseBody
    public Map addThree(String stName,Integer stSort,String stIdTwo){
        return typeService.addThree(stName,stSort,stIdTwo);
    }

    /**
     * 跳转到分类列表页面
     * @return
     */
    @RequestMapping(value = "findAll",method = RequestMethod.GET)
    public String findAll(HttpServletRequest request, String glId){
        //查询该子导航的增删改查的权限
        DicGroupLimit model = groupLimitService.queryByUUID(glId);

        TypeVo typeVo =  new TypeVo();
        typeVo.setStFid("0");
        typeVo.setStLevel(1);
        List<TypeVo> list=typeService.findByWhere(typeVo);

        for (TypeVo vo : list){
            //顶级分类的id 作为查询下一级的父id条件
            String stFid = vo.getStId();
            TypeVo typeVo1 =  new TypeVo();
            typeVo1.setStFid(stFid);
            typeVo1.setStLevel(2);
            List<TypeVo> list1=typeService.findByWhere(typeVo1);
            vo.setList(list1);
            for (TypeVo typeVo2 : list1) {
                List<TypeVo> list2=typeService.findTypeByStFid1(typeVo2.getStId());
                typeVo2.setList(list2);
            }
        }

        request.setAttribute("list",list);
        request.setAttribute("model",model);
        request.setAttribute("url", PropertiesUtil.getProperty("URL"));
        return "type/list";
    }


//    @RequestMapping(value = "findAll",method = RequestMethod.GET)
//    public String findAll(HttpServletRequest request, String glId){
//        //查询该子导航的增删改查的权限
//        DicGroupLimit model = groupLimitService.queryByUUID(glId);
//        request.setAttribute("model",model);
//        request.setAttribute("url", PropertiesUtil.getProperty("URL"));
//        return "type/list";
//    }

    /**
     * 获取列表数据
     * @return
     */
    @RequestMapping(value = "findAllJson",method = RequestMethod.GET)
    @ResponseBody
    public Object findAllJson(Integer page,Integer limit){
        TypeVo typeVo =  new TypeVo();
        typeVo.setStFid("0");
        typeVo.setStLevel(1);
        //查询顶级分类
        Integer count=typeService.findByWhere(typeVo).size();

        if (page >0 & limit >0){
            PageHelper.startPage(page,limit,false);
        }

        List<TypeVo> list=typeService.findByWhere(typeVo);
        return JsonData.returnLayuiMap(list, count);
    }

}
